/*
 * @Description: 
 * @Author: Dxcr
 * @Date: 2025-01-15 16:06:34
 * @LastEditTime: 2025-01-16 10:33:53
 * @LastEditors: Dxcr
 */
use std::fs;
use std::path::Path;
use std::io;

fn main() {
    // 获取项目根目录路径
    let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
    let release_dir = Path::new(&manifest_dir).join("target/release");

    // 定义要复制的文件
    let files = ["config.json", "data.csv"];

    // 调用复制函数
    if let Err(err) = copy_files_to_dir(&files, &release_dir) {
        eprintln!("Failed to copy files: {}", err);
    }

    // 告诉 Cargo，当文件修改时需要重新运行 build.rs
    for file in files {
        println!("cargo:rerun-if-changed={}", file);
    }
}
//复制多个文件到目录
fn copy_files_to_dir(files: &[&str], dest_dir: &Path) -> io::Result<()> {
    // 确保目标目录存在
    fs::create_dir_all(dest_dir)?;

    // 遍历文件并复制
    for file in files {
        let file_name = Path::new(file).file_name().ok_or_else(|| {
            io::Error::new(io::ErrorKind::InvalidInput, "Invalid file name")
        })?;
        let dest_path = dest_dir.join(file_name);
        fs::copy(file, &dest_path)?;
    }

    Ok(())
}